package com.example.lirisheng.springbootfinalexperiment.repositories;

import com.example.lirisheng.springbootfinalexperiment.entity.UserRole;
import com.example.lirisheng.springbootfinalexperiment.entity.Users;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;

import java.util.List;
import java.util.Set;

public interface UserRoleRepository  extends CrudRepository<UserRole,Integer> {

    public  void  deleteAllByUser(Long userId);

    public Set<UserRole> findAllByUser(Long userId);

    @Query(value = "select  name  from role JOIN user_role on role.id = user_role.role where  user_role.user=?1   " ,
            nativeQuery = true)
    public Set<String> findRoleByUserId(Long userId);


    @Query(value = "select  ur.user  from role as r JOIN user_role as ur on r.id = ur.role where r=?1 GROUP  By ur.user " ,
            nativeQuery = true)
    public Set<Integer> findUserIdByRole(String roleName);
}
